using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Xml;
using Microsoft.VisualBasic.Devices;

namespace TeamReview.SetupRegistrationAction
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Computer computer = new Computer();

            string addinName = Environment.CommandLine.Trim();
            Assembly asm = Assembly.GetExecutingAssembly();
            
            string fileName = computer.FileSystem.SpecialDirectories.Desktop;
            FileInfo fInfo;
            DirectoryInfo dirInfo;
            XmlDocument doc = new XmlDocument();
            string folderPath = "";

            //in this implementation, addinName in commandline cannot have a space in it
            addinName = addinName.Substring(addinName.LastIndexOf(" ") + 1).Trim();

            folderPath = Path.Combine(computer.FileSystem.SpecialDirectories.MyDocuments
                , @"Visual Studio 10\Addins");
            dirInfo = new DirectoryInfo(folderPath);

            if( ! dirInfo.Exists )
                dirInfo.Create();

            string assemblyPathandName = Path.Combine(
                Path.GetDirectoryName(asm.Location), addinName + ".DLL");

            // get directory name of install directory
            fileName = Path.Combine(Path.GetDirectoryName(asm.Location), 
              addinName + ".Addin");

            fInfo = new FileInfo(fileName);

            if (!fInfo.Exists)
                throw new Exception("File '" + fileName + "' is not found. Installation will be aborted or not complete.");
            else
            {
                // Load the .Addin file into XML document
                try
                {
                    doc.Load(fInfo.FullName);
                    // search for the assembly element which points to the addin dll
                    bool found = false;
                    foreach (XmlNode node in doc["Extensibility"]["Addin"])
                    {
                        if (node.Name == "Assembly")
                        {
                            // Change the Assembly to point to where the Addin is installed
                            node.InnerText = assemblyPathandName;
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                        throw new Exception("XML File is missing the Assembley Element, installation is not complete");

                    // Save the updated xml .addin File
                    doc.Save(fInfo.FullName);

                    // Copy the file to the target path
                    string destFile = Path.Combine(folderPath, addinName + ".Addin");
                    fInfo.CopyTo(Path.Combine(folderPath, destFile), true);

                }
                catch (Exception ex)
                {
                    throw new Exception("Failure to process file '" + 
                        fInfo.FullName + "'. Error= " + ex.ToString() + 
                        ". Installation will be aborted or not complete.");

                }
            }
        }
    }
}